﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class DonHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["AdminName"] != null)
            {
                DisplayList();
            }
            else
                Response.Redirect("Default.aspx");            
        }
        
    }
    protected void DisplayList()
    {
        string sql = string.Format(@"
                                        SELECT
                                            b.OrderID,
                                            a.FullName,
                                            a.SDT,
                                            b.NgayDat,
                                            b.NgayNhan,
                                            b.NoiNhan,
                                            b.TrangThai 
                                        FROM
                                            customers a INNER JOIN Orders b 
                                                        ON a.CustomerID = b.CustomerID
                                        Order by b.NgayDat DESC
                                        ");
        DataTable dt = new DataTable();
        dt = DataBase.GetDataTable(sql);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;
        string id = GridView1.Rows[index].Cells[3].Text;
        string sql = "Delete from OrderDetail where OrderID='" + id + "'";
        string sql1 = " Delete from Orders where OrderID='" + id + "'";
        try
        {
            DataBase.ExecuteNonQuery(sql);
            DataBase.ExecuteNonQuery(sql1);
            DisplayList();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int index = e.NewEditIndex;
        MultiView1.ActiveViewIndex = 1;
        txtID.Text = GridView1.Rows[index].Cells[3].Text;
        string sql = string.Format(@"
                                     SELECT
                                            b.OrderID,
                                            a.FullName,
                                            a.SDT,
                                            b.NgayDat,
                                            b.NgayNhan,
                                            b.NoiNhan,
                                            b.TrangThai 
                                     FROM
                                            customers a INNER JOIN Orders b 
                                                        ON a.CustomerID = b.CustomerID                                                
                                     WHERE b.OrderID={0}
                                                ", txtID.Text);
        DataTable dt = DataBase.GetDataTable(sql);
        txtTenKH.Text = dt.Rows[0]["FullName"].ToString();
        txtNoiNhan.Text = dt.Rows[0]["NoiNhan"].ToString();
        txtSDT.Text = dt.Rows[0]["SDT"].ToString();
        string trangthai = dt.Rows[0]["TrangThai"].ToString();
        if (trangthai == "Đặt hàng")
            cmbTrangThai.SelectedIndex = 0;
        else if (trangthai == "Đã thanh toán")
                cmbTrangThai.SelectedIndex = 2;
            else
                cmbTrangThai.SelectedIndex = 1;       
       
    }
    protected void btnList_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.DisplayList();
    }
    protected void btnFind_Click(object sender, EventArgs e)
    {
        string sql = string.Format(@"
                                        SELECT
                                            b.OrderID,
                                            a.FullName,
                                            a.SDT,
                                            b.NgayDat,
                                            b.NgayNhan,
                                            b.NoiNhan,
                                            b.TrangThai 
                                        FROM
                                            customers a INNER JOIN Orders b 
                                                        ON a.CustomerID = b.CustomerID
                                        WHERE a.FullName like N'%{0}%'
                                        ",txtCustomerName.Text);
        DataTable dt = new DataTable();
        dt = DataBase.GetDataTable(sql);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {        
        string sqlorder = string.Format(@"UPDATE Orders SET NoiNhan=N'{0}',
                                                            TrangThai=N'{1}'
                                          WHERE OrderID = {2}", txtNoiNhan.Text
                                                              , cmbTrangThai.SelectedItem.ToString()
                                                              , txtID.Text);
        DataBase.ExecuteNonQuery(sqlorder);
        this.DisplayList();
        MultiView1.ActiveViewIndex = 0;
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int index = e.NewSelectedIndex;
        txtID.Text = GridView1.Rows[index].Cells[3].Text;
        this.DetailDisplay();
        MultiView1.ActiveViewIndex = 2;        
    }
    protected void DetailDisplay()
    {
        string sql = "SELECT od.MaXe,tt.TenXe,od.GiaBan,od.SoLuongMua FROM OrderDetail od INNER JOIN ThongTinCoBan tt ON od.MaXe = tt.MaXe WHERE od.OrderID=@id";
        DataTable dt = DataBase.GetDataTable(sql, "@id", txtID.Text);
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = e.RowIndex;
        string maxe = GridView2.Rows[index].Cells[1].Text;
        string sql = "DELETE FROM OrderDetail WHERE OrderID=@orderid AND MaXe=@maxe";
        DataBase.ExecuteNonQuery(sql, "@orderid", txtID.Text, "@maxe", maxe);
        this.DetailDisplay();
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
        this.DisplayList();
    }
}
